<template>
  <div class="loop-node">
    <!-- 删除按钮 -->
    <el-button 
      class="delete-btn" 
      size="small" 
      type="danger" 
      circle
      @click.stop="handleDelete"
    >
      <el-icon><Close /></el-icon>
    </el-button>
    
    <div class="node-header">
      <el-icon class="node-icon"><Refresh /></el-icon>
      <span class="node-title">{{ data.label }}</span>
    </div>
    <div class="node-content">
      <div class="loop-info" v-if="data.config?.type">
        <span class="loop-type">{{ getLoopTypeLabel() }}</span>
        <span class="loop-detail">{{ getLoopDetail() }}</span>
      </div>
      <div class="node-description" v-else>
        点击配置循环
      </div>
    </div>
    
    <!-- 连接点 -->
    <Handle
      type="target"
      position="top"
      class="custom-handle target-handle"
    />
    <Handle
      type="source"
      position="bottom"
      class="custom-handle source-handle"
    />
    <!-- 循环体连接点 -->
    <Handle
      type="source"
      position="right"
      class="custom-handle source-handle loop-body-handle"
      data-label="循环体"
    />
  </div>
</template>

<script setup>
import { Handle } from '@vue-flow/core'
import { Refresh, Close } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const props = defineProps({
  data: {
    type: Object,
    required: true
  },
  id: {
    type: String,
    required: true
  }
})

const emit = defineEmits(['delete'])

// 获取循环类型标签
const getLoopTypeLabel = () => {
  const typeMap = {
    'count': '计数循环',
    'while': '条件循环',
    'foreach': '遍历循环'
  }
  return typeMap[props.data.config?.type] || '未知'
}

// 获取循环详情
const getLoopDetail = () => {
  const config = props.data.config
  if (!config) return ''
  
  switch (config.type) {
    case 'count':
      return `${config.count || 1} 次`
    case 'while':
      return config.condition ? `条件: ${config.condition.substring(0, 15)}...` : '未设置条件'
    case 'foreach':
      return config.array ? `遍历: ${config.array}` : '未设置数组'
    default:
      return ''
  }
}

// 处理删除节点
const handleDelete = () => {
  ElMessageBox.confirm(
    '确定要删除这个循环节点吗？此操作不可撤销。',
    '删除节点',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    emit('delete', { id: props.id, ...props })
    ElMessage.success('节点已删除')
  }).catch(() => {
    // 用户取消删除
  })
}
</script>

<style scoped>
.loop-node {
  background: linear-gradient(135deg, #909399, #b3b7c1);
  color: white;
  border-radius: 12px;
  padding: 12px 16px;
  min-width: 140px;
  max-width: 180px;
  box-shadow: 0 4px 12px rgba(144, 147, 153, 0.3);
  position: relative;
  border: 2px dashed rgba(255, 255, 255, 0.3);
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.node-icon {
  font-size: 16px;
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.node-title {
  font-weight: 600;
  font-size: 14px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.node-content {
  font-size: 12px;
}

.loop-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.loop-type {
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
}

.loop-detail {
  color: rgba(255, 255, 255, 0.7);
  font-size: 11px;
}

.node-description {
  color: rgba(255, 255, 255, 0.8);
  font-style: italic;
}

.custom-handle {
  width: 12px;
  height: 12px;
  border: 2px solid white;
  background: #909399;
}

.target-handle {
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
}

.source-handle {
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
}

.loop-body-handle {
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  background: #67c23a;
  border-color: #67c23a;
}

/* 循环体标签 */
.loop-body-handle::after {
  content: '↻';
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #67c23a;
  font-size: 14px;
  font-weight: bold;
}

.delete-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px !important;
  height: 20px !important;
  min-height: 20px !important;
  padding: 0 !important;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.loop-node:hover .delete-btn {
  opacity: 1;
}

.delete-btn .el-icon {
  font-size: 12px;
}
</style>